package com.emily.test.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 * @author  田庆新
 */
public class DateUtil {

    /**
     * 获取当前时间
     * @return 当前时间
     */
    public static Date getNow(){
        Date date= new Date();
        //long t=System.currentTimeMillis();
        //date.setTime(t);
        return date;
    }

    /**
     *获取当前时间(格式为字符串)
     * @return 当前时间的字符串格式
     */
    public static String getNowStr(){
        Date date=new Date();
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return  sf.format(date);
    }

    /**
     * 根据format格式获得字符串日期
     *
     * @param format
     * @param operateTime
     * @return
     */
    public static String getDate(String format, String operateTime) {

        String strDate = new SimpleDateFormat(format).format(DateUtil.toDate("yyyy-MM-dd hh:mm:ss", operateTime));
        return strDate;
    }

    /**
     * 字符串转换成为java.util.Date
     *
     * @param sDate
     *            字符串
     * @param sFmt
     *            format
     * @return Date java.util.Date日期
     */
    public static Date toDate(String sFmt, String sDate) {
        Date dt = null;
        try {
            dt = new SimpleDateFormat(sFmt).parse(sDate);
        } catch (ParseException e) {
            return dt;
        }
        return dt;
    }

    /**
     * 获取当前时间的时间戳
     * @return
     */
    public static long getNowTimeInMillis(){
        return new  Date().getTime();
    }

    /**
     * 获取当月第一天日期
     * @return
     */
    public static String getFirstDayOfMonth(){
        String str = "";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.DATE,1);//设为当前月的1 号
        str=sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * 获取上月最后一天
     * @return
     */
    public static String getPreviousMonthEnd(){
        String str = "";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.add(Calendar.MONTH,-1);//减一个月
        lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天
        lastDate.roll(Calendar.DATE, -1);//日期回滚一天，也就是本月最后一天
        str=sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * 获取当年的第一天
     * @return
     */
    public static Date getCurrYearFirst(){
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * 获取当年的最后一天
     * @return
     */
    public static Date getCurrYearLast(){
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearLast(currentYear);
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }

}
